package SSM.Chat.Filter;

import SSM.Chat.Utils.Request;
import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// MVC自带的拦截器
//@WebFilter // 用于修饰普通拦截器，代替web.xml
@WebFilter("/*")
public class filter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain Chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) resp;
        response.setHeader("Access-Control-Allow-Origin", "*");//指定授权访问的域，*允许所有url访问
        response.setHeader("Access-Control-Allow-Methods", "*");//授权请求的方法
        response.setHeader("Access-Control-Max-Age", "3600");//60 min
        response.setHeader("Access-Control-Allow-Headers", "*");//响应头部
        response.setHeader("Access-Control-Allow-Credentials", "true");//允许客户端携带验证信息
        Chain.doFilter(req, resp);//通过过滤器后，继续执行原本请求
    }

    @Override
    public void destroy() {

    }
}